﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace TileMapEditor.TileEngine
{
    public class Map
    {
        #region ATTRIBUTES

        private int width, height;
        private int tileWidth, tileHeight;
        private GridLayer grid;
        private List<Layer> layers;
        #endregion

        #region PROPERTIES

        public GridLayer Grid 
        {
            get { return grid; }
        }

        public int WidthInPixels 
        {
            get { return tileWidth * width; }
        }

        public int HeightInPixels 
        {
            get { return tileHeight * height; }
        }

        #endregion

        public Map(int w, int h, int tileW, int tileH) 
        {
            width = w;
            height = h;
            tileWidth = tileW;
            tileHeight = tileH;
            layers = new List<Layer>();
        }

        public void Draw(SpriteBatch spriteBatch, Camera cam, MouseState curState, MouseState prevState) 
        {
            if (grid != null && grid.IsVisible)
            {
                //grid on bottom
                if (!grid.OnTop)
                {
                    grid.Draw(spriteBatch);

                    foreach (Layer l in layers)
                    {
                        if (l.IsVisible)
                            l.Draw(spriteBatch, cam, curState, prevState);
                    }
                }
                //grid on top
                else if (grid.OnTop)
                {
                    foreach (Layer l in layers)
                    {
                        if (l.IsVisible)
                            l.Draw(spriteBatch, cam, curState, prevState);
                    }

                    grid.Draw(spriteBatch);
                }
            }
            //no grid
            else 
            {
                foreach (Layer l in layers)
                {
                    if (l.IsVisible)
                        l.Draw(spriteBatch, cam, curState, prevState);
                }
            }


        }

        public void AddLayer(Layer layer) 
        {
            layers.Add(layer);   
        }

        public void SetGrid(Texture2D gridTexture) 
        {
            grid = new GridLayer(width, height, tileWidth, tileHeight, gridTexture); 
        }


    }

    class MapWriter
    {

    }

    class MapReader 
    {

    }
}

